package com.atguigu.yygh.service.impl;

import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.exception.YyghException;
import com.atguigu.yygh.mapper.HospitalSetMapper;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.result.ResultCodeEnum;
import com.atguigu.yygh.service.HospitalSetService;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.atguigu.yygh.vo.order.SignInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Queue;
import java.util.Random;

/**
 * @author zr
 * @date 2021/12/7 21:06
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {
    @Resource
    private HospitalSetMapper hospitalSetMapper;

    @Override
    public void findPageHospSet(Page<HospitalSet> page, HospitalSetQueryVo hospitalSetQueryVo) {
        // 创建条件对象
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        // 判断对象是否为空
        if (hospitalSetQueryVo == null) {
            // 查询所有
            hospitalSetMapper.selectList(null);
        }
        // 取出医院名称
        String hosname = hospitalSetQueryVo.getHosname();
        // 取出医院编号
        String hoscode = hospitalSetQueryVo.getHoscode();
        // 判断是否为空
        if (StringUtils.hasText(hosname)) {
            queryWrapper.like("hosname", hosname);
        }
        if (StringUtils.hasText(hoscode)) {
            queryWrapper.like("hoscode", hoscode);
        }
        hospitalSetMapper.selectPage(page, queryWrapper);
    }

    @Override
    public boolean saveHospitalSet(HospitalSet hospitalSet) {
        // 0不可以 1 可以用
        hospitalSet.setStatus(1);
        // 设置 签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        int insert = baseMapper.insert(hospitalSet);
        return insert > 0;
    }

    @Override
    public String getHosCode(String hoscode) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("hoscode", hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(queryWrapper);
        if (hospitalSet == null) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        return hospitalSet.getSignKey();
    }

    /**
     * 根据医院编号获取医院签名信息
     *
     * @param hosCode
     * @return
     */
    @Override
    public SignInfoVo getSignInfoVo(String hosCode) {
        SignInfoVo signInfoVo = new SignInfoVo();
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hoscode", hosCode);
        HospitalSet hospitalSet = baseMapper.selectOne(queryWrapper);
        if (hospitalSet == null) {
            throw new YyghException(ResultCodeEnum.DATA_ERROR);
        }
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        signInfoVo.setSignKey(hospitalSet.getSignKey());
        return signInfoVo;
    }
}
